package org.p2psimulator.analysis.jfreechart;

import java.awt.Color;
import java.awt.Font;
import java.awt.HeadlessException;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.chart.title.LegendTitle;
import org.jfree.data.general.Dataset;
import org.jfree.data.xy.XYDataset;


@SuppressWarnings("serial")
public class LineChartBoard extends ChartBoard {

	static final Color colorList[] = { Color.blue, Color.DARK_GRAY, Color.red,
			Color.green, Color.orange, Color.black };

	public LineChartBoard(String title, Dataset dataset, String xlabel,
			String ylabel) throws HeadlessException {

		super(title, dataset, xlabel, ylabel);


	}

	public JFreeChart createChart(Dataset dataset, String xlabel, String ylabel) {
		// create the chart...
		// JFreeChart chart = ChartFactory.createLineChart(super.getTitle(), //
		// chart
		// // title
		// xlabel, // domain axis label
		// ylabel, // range axis label
		// (CategoryDataset) dataset, // data
		// PlotOrientation.VERTICAL, // orientation
		// true, // include legend
		// true, // tooltips
		// false // urls
		// );

		JFreeChart c = ChartFactory.createXYLineChart(
				super.getTitle(), // chart title
				xlabel, // domain axis label
				ylabel, // range axis label
				(XYDataset) dataset, // data
				PlotOrientation.VERTICAL, // orientation
				true, // include legend
				true, // tooltips
				false // urls
				);
		c.setBackgroundPaint(Color.lightGray);

		LegendTitle leg = c.getLegend();
		leg.setItemFont(new Font(Font.SANS_SERIF, Font.BOLD, 9));
		XYPlot plot = (XYPlot) c.getPlot();
		plot.setBackgroundPaint(Color.white);
		plot.setRangeGridlinePaint(Color.lightGray);
		plot.setDomainGridlinePaint(Color.lightGray);
		plot.setDomainGridlinesVisible(true);
		plot.setRangeGridlinesVisible(true);

		// customise the range axis...
		NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
		rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
		rangeAxis.setLabelFont(new Font(Font.SANS_SERIF, Font.BOLD, 6));

		NumberAxis domainAxis = (NumberAxis) plot.getDomainAxis();
		domainAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
		domainAxis.setLabelFont(new Font(Font.SANS_SERIF, Font.BOLD, 6));

		// customise the renderer...
		XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) plot
				.getRenderer();
		renderer.setDrawOutlines(true);
		renderer.setUseFillPaint(true);
		renderer.setBaseShapesVisible(true);
		renderer.setBaseFillPaint(Color.gray);

		for (int i = 0; i < colorList.length; i++) {
			renderer.setSeriesPaint(i, colorList[i]);
		}

		return c;
	}
}
